package com.pinc.poop;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBClass extends SQLiteOpenHelper {

	private static final int DATABASE_VERSION = 2;
	private static final String DATABASE_NAME = "DB_PUPU";
	public static final String TABLE_NAME_PUPU = "pupu_rec";
	//	private static final String PUPU_TABLE_CREATE =
	//		"CREATE TABLE " + TABLE_NAME_PUPU + " (" +
	//		"_id INTEGER PRIMARY KEY, " +
	//		"color INTEGER, " +
	//		"shape INTEGER, " +
	//		"date TEXT, " +
	//		"time TEXT); ";

	private static final String PUPU_TABLE_CREATE_V2 =
		"CREATE TABLE " + TABLE_NAME_PUPU + " (" +
		"_id INTEGER PRIMARY KEY, " +
		"color INTEGER, " +
		"shape INTEGER, " +
		"date TEXT, " +
		"time TEXT, " +
		"amount INTEGER DEFAULT 3, " +
		"memo TEXT DEFAULT '');";

	public SQLiteDatabase mDB;

	public DBClass(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) { 	
		db.execSQL(PUPU_TABLE_CREATE_V2);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		if ( oldVersion <= 1)
		{
			db.execSQL("ALTER TABLE " + TABLE_NAME_PUPU + " ADD COLUMN amount INTEGER DEFAULT 3");
			db.execSQL("ALTER TABLE " + TABLE_NAME_PUPU + " ADD COLUMN memo TEXT DEFAULT ''");
		}
	}

	public void putRecord(String datestr, String timestr)
	{
		mDB = this.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put("date", datestr);
		cv.put("time", timestr);
		mDB.insert(TABLE_NAME_PUPU, null, cv);
		mDB.close();
	}

	public Cursor getRecord(String whereclause, String[] args, String orderby)
	{
		mDB = this.getReadableDatabase();
		Cursor cs = mDB.query(TABLE_NAME_PUPU, null, whereclause, args, null, null, orderby);
		return cs;
	}

	public void delRecord(String datestr, String timestr)
	{
		mDB = this.getWritableDatabase();
		mDB.delete(TABLE_NAME_PUPU, "date = ? AND time = ?", new String[]{datestr, timestr});
		mDB.close();
	}

	public void updateRecordDepricate(String datestr, String timestr, String newtime)
	{
		mDB = this.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put("time", newtime);
		mDB.update(TABLE_NAME_PUPU, cv, "date = ? AND time = ?", new String[]{datestr, timestr});
		mDB.close();
	}

	public void updateRecord(int id, String newtime, int color, int shape, int amount, String note)
	{
		mDB = this.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put("time", newtime);
		cv.put("color", color);
		cv.put("shape", shape);
		cv.put("amount", amount);
		cv.put("memo", note);
		mDB.update(TABLE_NAME_PUPU, cv, "_id = ?", new String[]{String.format("%d", id)});
		mDB.close();
	}

	public int getRecordCount(String whereclause, String[] args)
	{
		int cnt;
		mDB = this.getReadableDatabase();
		Cursor cs = mDB.query(TABLE_NAME_PUPU, null, whereclause, args, null, null, null);
		cnt = cs.getCount();
		cs.close();
		mDB.close();
		return cnt;
	}
}
